package org.aurora.one.noticeAndWait_2;

import org.aurora.myQueue.MyBlockingQueue;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.BlockingQueue;

import static org.aurora.myQueue.MyBlockingQueue.blockingQueue;

/**
 * @author : Aurora
 * @date : 2024/1/21 22:30
 * @description
 */
@Component
public class ConsumerExample implements Runnable{
    @Override
    public void run() {
        while(true){
            synchronized (blockingQueue) {
                while (blockingQueue.size() == 0) {
                    try {
                        blockingQueue.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                Object remove = blockingQueue.remove();
                System.out.println("消费了：" + remove);
                blockingQueue.notifyAll();
            }
        }
    }
}
